const db = wx.cloud.database();
const app = getApp();
const tab = require("../../utils/tab.js");
const common = require("../../utils/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    userInfo: null
  },
  async upLoadImg(path) {
    let imgName = new Date().getTime() + "_" + Math.random();
    let extName = path.slice(path.indexOf("."))
    let res = await wx.cloud.uploadFile({
      cloudPath: "orderImg/" + imgName + extName,
      filePath: path,
    })
    return res.fileID
  },
  async getImgList(tempFile) {
    let promise = await tempFile.map(async item => {
      return await this.upLoadImg(item);
    })
    let imgList = await Promise.all(promise);
    return imgList;
  },
  // 添加图片
  async addImg() {
    wx.chooseImage({
      count: 4-this.data.imgList.length,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
    }).then(async res => {
      wx.showLoading({
        title: '正在加载',
      })
      // 上传到云存储
        let imgList = await this.getImgList(res.tempFilePaths);
        this.setData({
          imgList: this.data.imgList.concat(imgList)
        })
      wx.hideLoading();
    }).catch(e=>{})
  },
  // 删除图片
  delImg(e) {
    let fileName = e.currentTarget.dataset.filename;
    let index = e.currentTarget.dataset.index;
    wx.cloud.deleteFile({
      fileList: [fileName],
      success: res => {
        this.data.imgList.splice(index, 1)
        this.setData({
          imgList: this.data.imgList
        })
      },
      fail: err => {
        console.log(err);
      }
    })
  },
  // 发布订单
  submitOrder(res) {
    let obj = res.detail.value;
    if (!obj.detail) {
      wx.showToast({
        title: '请填写问题描述',
        icon: 'none'
      })
    } else {
      obj.num = this.data.userInfo.num;
      obj.name = this.data.userInfo.name;
      obj.phone = this.data.userInfo.phone;
      obj.building_num = this.data.userInfo.building_num;
      obj.room_num = this.data.userInfo.room_num;
      obj.imgList = this.data.imgList;
      obj.createTime = new Date().getTime();
      // 订单状态 0-待审核 1-待完成 2-待结单 3-待评价 4-已完成
      obj.status = 0;
      obj.statusLabel = app.globalData.statusList[obj.status]
      common.addData("orderList", obj, "add")
    }
  },
  // 重置订单
  resetOreder(e) {
    this.setData({
      imgList: []
    })
  },
  getInfo() {
    db.collection("userList").where({
      num: app.globalData.num
    }).get().then(res => {
      this.setData({
        userInfo: res.data[0]
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInfo();
    tab.updateRoleTab(this, "user");
    tab.updateTabIndex(this, 1)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})